#!/bin/bash

cur_user=`whoami`
sudo_user=""
if [ $1 = "test" ]; then
hosts=(123.57.148.137)
cp config/test.params.php config/params.php
cp config/test.web.php config/web.php
cp config/test.db.php config/db.php
cp web/test.index.php web/index.php
#cp nginx/test.nginx.conf nginx/nginx.conf
fi
if [ $1 = "prod" ]; then
hosts=(10.51.62.143 10.44.175.139)
cp config/prod.params.php config/params.php
cp config/prod.web.php config/web.php
cp config/prod.db.php config/db.php
cp web/prod.index.php web/index.php
#cp nginx/prod.nginx.conf nginx/nginx.conf
fi

src_tgz=`pwd`"/deploy/bkphp_admin.tar"
echo "开始打包..."
#tar -cvf $src_tgz assets/ vendor/ yii yii.bat web/ commands/ config/ controllers/ deploy/ mail/ nginx/ proxy/ helper/
tar -cvf $src_tgz assets/ web/ commands/ config/ controllers/ deploy/ mail/ proxy/ helper/ models/ 
#tar -cvf $src_tgz assets/ web/ commands/ config/ controllers/ deploy/ mail/ proxy/ helper/ models/
echo "打包完成"
if [[ ! -s ${src_tgz} ]]; then
    echo "打包失败"
    exit;
fi

#发布到Mac本地apache路径/Library/WebServer/Documents/qly-admin
if [ $1 = "local" ]; then
    echo "发布到本地: /Library/WebServer/Documents/qly-admin"
    tar -xf $src_tgz -C /Library/WebServer/Documents/qly-admin
fi
#
#######################################上线#########################################################
cur_user=`whoami`
sudo_user=""
if [ "$cur_user" != "work" ]; then
sudo_user="sudo -u work"
fi
echo $sudo_user

for host in ${hosts[@]}
do
    echo "deploying php to $1 : $host ..."
    $sudo_user scp $src_tgz work@${host}:/home/work/deploy_php_history
    $sudo_user ssh work@$host "tar -xf /home/work/deploy_php_history/bkphp_admin.tar -C /home/work/bkshop_admin;rm -rf /home/work/bkshop_admin/.git"
done
